Completed
Push — master ( 19fb27...7e6a94 )
by Johan
01:16
created

JsBarcode.js ➔ API   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 1
rs 10
1
// Import all the barcodes
2
import barcodes from './barcodes/';
3
4
// Import the renderers
5
import renderers from './renderers/';
6
7
// Help functions
8
import merge from './help/merge.js';
9
import linearizeEncodings from './help/linearizeEncodings.js';
10
import fixOptions from './help/fixOptions.js';
11
import getRenderProperties from './help/getRenderProperties.js';
12
13
// Default values
14
import defaults from './defaults/defaults.js';
15
16
// The protype of the object returned from the JsBarcode() call
17
let API = function(){};
18
19
// The first call of the library API
20
// Will return an object with all barcodes calls and the data that is used
21
// by the renderers
22
let JsBarcode = function(element, text, options){
23
	var api = new API();
24
25
	if(typeof element === "undefined"){
26
		throw Error("No element to render on was provided.");
27
	}
28
29
	// Variables that will be pased through the API calls
30
	api._renderProperties = getRenderProperties(element);
31
	api._encodings = [];
32
	api._options = defaults;
33
34
	// If text is set, use the simple syntax (render the barcode directly)
35
	if(typeof text !== "undefined"){
36
		options = options || {};
37
38
		if(!options.format){
39
			options.format = autoSelectBarcode();
40
		}
41
42
		api.options(options);
43
		api[options.format](text, options);
44
		api.render();
45
	}
46
47
	return api;
48
};
49
50
// To make tests work TODO: remove
51
JsBarcode.getModule = function(name){
52
	return barcodes[name];
53
};
54
55
// Register all barcodes
56
for(var name in barcodes){
57
	if(barcodes.hasOwnProperty(name)){ // Security check if the propery is a prototype property
58
		registerBarcode(barcodes, name);
59
	}
60
}
61
function registerBarcode(barcodes, name){
62
	API.prototype[name] =
63
	API.prototype[name.toUpperCase()] =
64
	API.prototype[name.toLowerCase()] =
65
	function(text, options){
66
		var newOptions = merge(this._options, options);
67
		var Encoder = barcodes[name];
68
		var encoded = encode(text, Encoder, newOptions);
69
		this._encodings.push(encoded);
70
71
		return this;
72
	};
73
}
74
75
// encode() handles the Encoder call and builds the binary string to be rendered
76
function encode(text, Encoder, options){
77
	// Ensure that text is a string
78
	text = "" + text;
79
80
	var encoder = new Encoder(text, options);
81
82
	// If the input is not valid for the encoder, throw error.
83
	// If the valid callback option is set, call it instead of throwing error
84
	if(!encoder.valid()){
85
		if(options.valid === defaults.valid){
86
			throw new Error('"' + text + '" is not a valid input.');
87
		}
88
		else{
89
			options.valid(false);
90
		}
91
	}
92
93
	// Make a request for the binary data (and other infromation) that should be rendered
94
	var encoded = encoder.encode();
95
96
	// Encodings can be nestled like [[1-1, 1-2], 2, [3-1, 3-2]
97
	// Convert to [1-1, 1-2, 2, 3-1, 3-2]
98
	encoded = linearizeEncodings(encoded);
99
100
	// Merge
101
	for(let i = 0; i < encoded.length; i++){
102
		encoded[i].options = merge(options, encoded[i].options);
103
	}
104
105
	return encoded;
106
}
107
108
function autoSelectBarcode(){
109
	// If CODE128 exists. Use it
110
	if(barcodes["CODE128"]){
111
		return "CODE128";
112
	}
113
114
	// Else, take the first (probably only) barcode
115
	return Object.keys(barcodes)[0];
116
}
117
118
// Sets global encoder options
119
// Added to the api by the JsBarcode function
120
API.prototype.options = function(options){
121
	this._options = merge(this._options, options);
122
	return this;
123
};
124
125
// Will create a blank space (usually in between barcodes)
126
API.prototype.blank = function(size){
127
	var zeroes = "0".repeat(size);
128
	this._encodings.push({data: zeroes});
129
	return this;
130
};
131
132
// Initialize JsBarcode on all HTML elements defined.
133
API.prototype.init = function(){
134
	// Make sure renderProperies is an array
135
	if(!Array.isArray(this._renderProperties)){
136
		this._renderProperties = [this._renderProperties];
137
	}
138
139
	var renderProperty;
140
	for(let i in this._renderProperties){
141
		renderProperty = this._renderProperties[i];
142
		var options = merge(this._options, renderProperty.options);
143
144
		if(options.format == "auto"){
145
			options.format = autoSelectBarcode();
146
		}
147
148
		var text = options.value;
149
150
		var Encoder = barcodes[options.format.toUpperCase()];
151
152
		var encoded = encode(text, Encoder, options);
153
154
		render(renderProperty, encoded, options);
155
	}
156
};
157
158
159
// The render API call. Calls the real render function.
160
API.prototype.render = function(){
161
	if(Array.isArray(this._renderProperties)){
162
		for(let i in this._renderProperties){
163
			render(this._renderProperties[i], this._encodings, this._options);
164
		}
165
	}
166
	else{
167
		render(this._renderProperties, this._encodings, this._options);
168
	}
169
170
	this._options.valid(true);
171
172
	return this;
173
};
174
175
// Prepares the encodings and calls the renderer
176
function render(renderProperties, encodings, options){
177
	var renderer = renderers[renderProperties.renderer];
178
179
	encodings = linearizeEncodings(encodings);
180
181
	for(let i = 0; i < encodings.length; i++){
182
		encodings[i].options = merge(options, encodings[i].options);
183
		fixOptions(encodings[i].options);
184
	}
185
186
	fixOptions(options);
187
188
	renderer(renderProperties.element, encodings, options);
189
190
	if(renderProperties.afterRender){
191
		renderProperties.afterRender();
192
	}
193
}
194
195
// Export to browser
196
if(typeof window !== "undefined"){
197
	window.JsBarcode = JsBarcode;
198
}
199
200
// Export to jQuery
201
/*global jQuery */
202
if (typeof jQuery !== 'undefined') {
203
	jQuery.fn.JsBarcode = function(content, options){
204
		var elementArray = [];
205
		jQuery(this).each(function() {
206
			elementArray.push(this);
207
		});
208
		return JsBarcode(elementArray, content, options);
209
	};
210
}
211
212
// Export to commonJS
213
module.exports = JsBarcode;
214